package com.git.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;

import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.serializer.SerializationException;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.git.domain.AjaxJson;
import com.git.domain.DemoEntity;
import com.git.mapper.DemoRedisDao;
import com.git.service.DemoService;
import com.google.gson.Gson;

@Controller
public class DemoController {
	@Autowired
	private DemoService demoService;
	
	
	@RequestMapping("putString")
	public ResponseEntity<AjaxJson> putString(String str){
		try {
			this.demoService.putString("str_key",str);
			return ResponseEntity.status(200).body(new AjaxJson(true,"success",null));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	@RequestMapping("putObject")
	public ResponseEntity<AjaxJson> putObject(){
		try {
			
			this.demoService.putObject("object_key",new DemoEntity(1, new Date()));
			return ResponseEntity.status(200).body(new AjaxJson(true,"success",null));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (SerializationException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	@RequestMapping("getString")
	public ResponseEntity<AjaxJson> getString(String key){
		try {
			String string = this.demoService.getString(key);
			return ResponseEntity.status(200).body(new AjaxJson(true,"success",string));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	@RequestMapping("getObject")
	public ResponseEntity<AjaxJson> getObject(String key){
		try {
			DemoEntity demoEntity = (DemoEntity) this.demoService.getObject(key);
			return ResponseEntity.status(200).body(new AjaxJson(true,"success",demoEntity));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@RequestMapping("addList")
	public void addList(String key) throws SerializationException, Exception{
		DemoRedisDao.putListObject(key, new Date(), 0);
	}
	@RequestMapping("getFirst")
	@ResponseBody
	public AjaxJson getFirst(String key) throws SerializationException, Exception{
		return new AjaxJson(true,"",DemoRedisDao.getListObjectByIndex(key, 0));
	}
	@RequestMapping("incr")
	@ResponseBody
	public AjaxJson incr(String key) throws SerializationException, Exception{
		DemoRedisDao.inc(key);
		return new AjaxJson(true,"",null);
	}
	
	
}
